<template>
    <div class="common-layout">
        <el-container>
            <el-header>
                <div class="logo-area">
                    <el-image class="custom-icon" :src="logoPath" fit="contain" :preview-src-list="[]"
                        @click.stop.prevent>
                        <template #error>
                            <div class="image-error">
                                <el-icon>
                                    <Picture />
                                </el-icon>
                            </div>
                        </template>
                    </el-image>
                    <span class="logo-text">PTEA</span>
                </div>

                <div class="header-content">
                    <div class="search-input"></div>
                    <el-menu mode="horizontal" class="top-menu" :default-active="activeIndex" router>
                        <el-menu-item index="/home/main">
                            <el-icon>
                                <House />
                            </el-icon>
                            <span>首页</span>
                        </el-menu-item>
                        <el-menu-item index="/home/order">
                            <el-icon>
                                <EditPen />
                            </el-icon>
                            <span>编辑写作</span>
                        </el-menu-item>
                        <el-menu-item index="/home/phone">
                            <el-icon>
                                <Phone />
                            </el-icon>
                            <span>联系我们</span>
                        </el-menu-item>
                    </el-menu>
                </div>
            </el-header>

            <el-container>
                <el-aside width="200px">
                    <el-menu :default-active="activeIndex" class="side-menu" router>
                        <el-menu-item index="/home/main">
                            <el-icon>
                                <Monitor />
                            </el-icon>
                            <span>首页</span>
                        </el-menu-item>
                        <el-menu-item index="/home/user">
                            <el-icon>
                                <User />
                            </el-icon>
                            <span>个人信息</span>
                        </el-menu-item>
                        <el-menu-item index="/home/order">
                            <el-icon>
                                <EditPen />
                            </el-icon>
                            <span>编辑写作</span>
                        </el-menu-item>
                        <el-menu-item index="/home/goods">
                            <el-icon>
                                <Files />
                            </el-icon>
                            <span>帖子列表</span>
                        </el-menu-item>
                        <el-menu-item index="/home/analytics">
                            <el-icon>
                                <Plus />
                            </el-icon>
                            <span>创建角色</span>
                        </el-menu-item>
                        <el-menu-item index="/home/notifications">
                            <el-icon>
                                <List />
                            </el-icon>
                            <span>角色列表</span>
                        </el-menu-item>
                        <!-- <el-menu-item index="/home/settings">
                            <el-icon>
                                <Setting />
                            </el-icon>
                            <span>系统设置</span>
                        </el-menu-item>
                        <el-menu-item index="/home/permissions">
                            <el-icon>
                                <Lock />
                            </el-icon>
                            <span>权限管理</span>
                        </el-menu-item>
                        <el-menu-item index="/home/logs">
                            <el-icon>
                                <Document />
                            </el-icon>
                            <span>日志查看</span>
                        </el-menu-item> -->
                    </el-menu>
                </el-aside>

                <el-main>
                    <router-view v-slot="{ Component }">
                        <keep-alive>
                            <component :is="Component" v-if="$route.meta.keepAlive" />
                        </keep-alive>
                        <component :is="Component" v-if="!$route.meta.keepAlive" />
                    </router-view>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import {
    House,
    Phone,
    Monitor,
    User,
    Setting,
    Lock,
    Document,
    Picture,
    Search,
    EditPen,
    List,
    Plus,
    Files
} from '@element-plus/icons-vue'
import logo from '@/assets/images/logo.png'
import { useRoute } from 'vue-router' // 添加路由钩子

const logoPath = ref(logo)
const route = useRoute()
const activeIndex = computed(() => route.path) // 使用计算属性绑定当前路由路径
</script>

<style scoped>
.el-header {
    background-color: #fff;
    border-bottom: 1px solid #eee;
    height: 60px;
    display: flex;
    align-items: center;
    padding: 0 20px;
}

.header-content {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo-area {
    display: flex;
    align-items: center;
    margin-right: 40px;
    min-width: 160px;
}

:deep(.custom-icon) {
    width: 36px;
    height: 36px;
    margin-right: 12px;
    transition: opacity 0.3s;
}

:deep(.custom-icon:hover) {
    opacity: 0.8;
    cursor: default;
}

.image-error {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
}

.logo-text {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    letter-spacing: 1px;
}

.search-input {
    width: 300px;
    margin-right: 30px;
}

/* 菜单项通用样式 */
.el-menu-item {
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s;
}

/* 顶部菜单样式 */
.top-menu {
    border-bottom: none;

    .el-icon {
        font-size: 1.1em;
        margin-right: 6px;
    }
}

/* 侧边栏菜单样式 */
.side-menu {
    border-right: none;
    height: calc(100vh - 60px);

    .el-icon {
        font-size: 1.3em;
        margin-right: 10px;
    }

    .el-menu-item {
        padding-left: 30px !important;

        &:hover {
            background-color: #f5f7fa;
        }
    }
}

.el-main {
    background-color: #f8f9fa;
    padding: 20px;
    min-height: calc(100vh - 60px);
}
</style>